package yanc.app

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class TypesController {
    def typesService
    static final Logger logger = LoggerFactory.getLogger(TypesController.class)

    def get() {
        def logoImg = request.getFile("images")
        def filePath = request.getSession().getServletContext().getRealPath("assets/${new Date().format("yyyyMMdd")}/")
        File f = new File(filePath)
        if (!f.exists()) {
            f.mkdirs()
        }
        def logoName = logoImg.getOriginalFilename()
        def fileSuffix = logoName.substring(logoName.lastIndexOf("."))
        def fileName = "${new Date().format("yyyyMMddHHmmss")}${fileSuffix}"
        def file = new File("${filePath}${fileName}")
        logoImg.transferTo(file)
        def path = "${new Date().format("yyyyMMdd")}/" + fileName
        render path
    }

    //公告 02 banner图 01  常见问题88
    def add() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def imgUrl = "${params.imgUrl}"
        def title = "${params.title}"
        def branch = new Types()
        bindData(branch, params, [exclude: ['page', 'pagesize', 'imgUrl', 'title']])
        try {
            typesService.add(branch, imgUrl, title)
            def list = Types.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Types.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("TypesController add Exception" + e.message)
        }
    }

    def getAll() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def title = "${params.title ?: ''}"
        def status = "${params.status ?: ''}"
        def types = "${params.types ?: ''}"
        def a = Types.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!title.isEmpty()) {
                    ilike("title", "%${title}%")
                }
                if (!status.isEmpty()) {
                    if (status == 'true') {
                        eq("status", true)
                    } else {
                        eq("status", false)
                    }
                }
                if (!types.isEmpty()) {
                    eq("types", types)
                }
            }
            order("modifyDate", "desc")
        }
        def b = Types.createCriteria()
        def length = b.get {
            and {
                if (!title.isEmpty()) {
                    ilike("title", "%${title}%")
                }
                if (!status.isEmpty()) {
                    if (status == 'true') {
                        eq("status", true)
                    } else {
                        eq("status", false)
                    }
                }
                if (!types.isEmpty()) {
                    eq("types", types)
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }

    def del() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def id = "${params.id ?: ''}"
        try {
            typesService.del(id)
            def list = Types.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Types.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("TypesController del Exception" + e.message)
        }
    }

    def getAlls() {
        def data = [:]
        def list = Types.list()
        data << [statuCode: "0", errMsg: "0", data: list]
        render data as JSON
    }

    def refresh() {
        def data = [:]
        def page = 1
        def pagesize = 20
        def id = "${params.id ?: ''}"
        def imgUrl = "${params.imgUrl ?: ''}"
        def title = "${params.title ?: ''}"
        def branch = Types.findById(id)
        bindData(branch, params, [exclude: ['id', 'imgUrl', 'title']])
        try {
            typesService.update(branch, imgUrl, title)
            def list = Types.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Types.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("TypesController refresh Exception" + e.message)
        }
    }
}
